//
//  YQLocationManager.m
//  YQLocation
//
//  Created by sz on 2018/11/12.
//  Copyright © 2018 yq. All rights reserved.
//

#import "YQLocationManager.h"
//导入高德的库

@interface YQLocationManager()<AMapLocationManagerDelegate,AMapSearchDelegate>

@property (nonatomic,strong) AMapLocationManager *locationManager;
@property (nonatomic,strong)AMapSearchAPI *mapSearch;
@property (nonatomic,copy) SearchPOICompleteBlock poiCompleteBlock;
@property (nonatomic,copy) SearchTipsCompleteBlock tipsCompleteBlock;


@end

@implementation YQLocationManager


#pragma mark -public func
- (void)startRequestLocationWithCompletionBlock:(LocatingCompletionBlock)completionBlock;{
    __weak typeof(self) weakSelf = self;
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        
        __strong typeof(self) strongSelf = weakSelf;
        if (!error) {
            strongSelf->_userLocaion = location;
            strongSelf->_reGeocode = regeocode;
        }
        if (completionBlock) {
            completionBlock(location, regeocode, error);
        }
    }];
}
- (void)searchAround:(NSInteger)pageNo location:(AMapGeoPoint *)point
searchPOICompeteBlock:(SearchPOICompleteBlock)completeBlock{
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
//    request.keywords = @"周边";
    request.keywords =  @"商务住宅";
    request.location = point;
    request.requireExtension = YES;
    request.page = pageNo < 1 ? 1 : pageNo;
    [self searchAroundWithRequest:request searchPOICompeteBlock:completeBlock];
}
- (void)searchUserAround:(NSInteger)pageNo
   searchPOICompeteBlock:(SearchPOICompleteBlock)completeBlock{
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    
    request.location = [AMapGeoPoint locationWithLatitude:self.userLocaion.coordinate.latitude
                                                longitude:self.userLocaion.coordinate.longitude];

    request.requireExtension = YES;
    request.page = pageNo < 1 ? 1 : pageNo;
    [self searchAroundWithRequest:request searchPOICompeteBlock:completeBlock];
}

- (void)searchAroundWithRequest:(AMapPOIAroundSearchRequest *)request
          searchPOICompeteBlock:(SearchPOICompleteBlock)completeBlock{
    self.poiCompleteBlock = completeBlock;
    [self.mapSearch AMapPOIAroundSearch:request];
}


- (void)tipsSearch:(NSString *)keywords searchTipsCompleteBlock:(SearchTipsCompleteBlock)tipsComleteBlock{
    AMapInputTipsSearchRequest *request = [[AMapInputTipsSearchRequest alloc] init];
    request.keywords = keywords;
    request.city = self.reGeocode.city;
    [self tipsSearchWithRequest:request searchTipsCompleteBlock:tipsComleteBlock];
}

- (void)tipsSearchWithRequest:(AMapInputTipsSearchRequest *)request
      searchTipsCompleteBlock:(SearchTipsCompleteBlock)tipsComleteBlock{
    self.tipsCompleteBlock = tipsComleteBlock;
    [self.mapSearch AMapInputTipsSearch:request];
}
#pragma mark -delegate
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
//    if (self.poiCompleteBlock) {
//        self.poiCompleteBlock(nil,error);
//    }
//    if (self.tipsCompleteBlock) {
//        self.tipsCompleteBlock(nil, error);
//    }
}

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
    if (self.poiCompleteBlock) {
        self.poiCompleteBlock(response,nil);
    }
}

- (void)onInputTipsSearchDone:(AMapInputTipsSearchRequest *)request response:(AMapInputTipsSearchResponse *)response{
    if (self.tipsCompleteBlock) {
        self.tipsCompleteBlock(response, nil);
    }
}
#pragma mark - setter getter
- (AMapLocationManager *)locationManager{
    if (!_locationManager) {
        _locationManager = [[AMapLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        //单次定位超时时间
        [_locationManager setLocationTimeout:6];
        [_locationManager setReGeocodeTimeout:3];
        
    }
    return _locationManager;
}

- (AMapSearchAPI *)mapSearch{
    if (!_mapSearch) {
        _mapSearch = [[AMapSearchAPI alloc] init];
        _mapSearch.delegate = self;
    }
    return _mapSearch;
}
@end
